﻿/*
 * copyright (c) 2009 hvbird
 *
 * This file is part of tdlib.
 *
 * tdlib is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * tdlib is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 */

using System;

namespace TDLib
{
    /// <summary>
    /// 表达式为true时，输出报告
    /// </summary>
    public class MCSException
    {
        public MCSException()
        {

        }
        /// <summary>
        /// 如果条件为真，则抛出例外
        /// </summary>
        /// <param name="boolExpression">条件表达式</param>
        /// <param name="strMessage">错误信息</param>
        public static void TrueThrow(bool boolExpression, string strMessage)
        {

            if (boolExpression == true)
            {
                throw new System.ApplicationException(strMessage);
            }

        }

        /// <summary>
        /// 向上一层投放异常
        /// </summary>
        /// <param name="ErrorID">错误代码</param>
        /// <param name="ErrorName">错误名称</param>
        /// <param name="oe">系统产生的异常</param>
        public static void ThrowError(int ErrorID, string ErrorName, System.Exception oe)
        {
            if ((ErrorID < 0) || (ErrorID > 9999))
            {
                ErrorID = 0;
            }

            string strErrorID = "0000" + ErrorID.ToString();
            strErrorID = strErrorID.Substring(strErrorID.Length - 5, 4);
            string strErrorInfo = "#" + strErrorID + ErrorName;
            if (oe != null)
                strErrorInfo += "(" + oe.Message + ")";

            throw new SystemException(strErrorInfo);
        }

        /// <summary>
        /// 获得异常的代码
        /// </summary>
        /// <param name="oe">异常对象</param>
        /// <returns>异常代码</returns>
        public static int GetExceptionCode(System.Exception oe)
        {
            int intResult = 0;
            if (oe.Message[1] == '#')
            {
                try
                {
                    intResult = int.Parse(oe.Message.Substring(2, 4));
                }
                catch
                {
                }
            }
            return intResult;
        }


        #region MSTools中错误代码列表 错误代码范围
        public const int _EDbaXmlTableDeleteError = 400;  //在删除XML虚拟表中的纪录时，发现要删除的纪录不在虚拟表中
        public const string _EDbaXmlTableDeleteErrorS = "在删除XML虚拟表中的纪录时，发现要删除的纪录不在虚拟表中";
        public const int _EDbaXmlTableGetFieldError = 401;
        public const string _EDbaXmlTableGetFieldErrorS = "在虚拟表中未发现字段";

        #endregion
    }
}
